package com.sunyard.client2.conf;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Properties;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import com.sunyard.client2.util.PropertiesUtil;

public class ConfServerPanel extends JPanel {
	private JTextField textField_ip;
	private JTextField textField_httpPort;
	private JTextField textField_socketPort;
	private JTextField textField_appName;
	private JTextField textField_groupName;
	private JTextField textField_userName;
	private JTextField textField_password;
	private JComboBox comboBox_connectionType;
	private JComboBox comboBox;
	private JTextField textField_1;

	/**
	 * Create the panel.
	 */
	public ConfServerPanel() {
		setLayout(null);

		JLabel lblNewLabel = new JLabel("IP:");
		lblNewLabel.setHorizontalAlignment(SwingConstants.RIGHT);
		lblNewLabel.setBounds(40, 52, 89, 15);
		add(lblNewLabel);

		textField_ip = new JTextField();
		textField_ip.setBounds(165, 49, 155, 21);
		textField_ip.setText(ConfUtil.getString("ip", ""));
		add(textField_ip);
		textField_ip.setColumns(10);

		JLabel lblHttpPort = new JLabel("Http Port:");
		lblHttpPort.setHorizontalAlignment(SwingConstants.RIGHT);
		lblHttpPort.setBounds(40, 90, 89, 15);
		add(lblHttpPort);

		textField_httpPort = new JTextField();
		textField_httpPort.setColumns(10);
		textField_httpPort.setBounds(165, 87, 155, 21);
		textField_httpPort.setText(ConfUtil.getString("httpPort", ""));
		add(textField_httpPort);

		JLabel lblSocketport = new JLabel("Socket Port:");
		lblSocketport.setHorizontalAlignment(SwingConstants.RIGHT);
		lblSocketport.setBounds(40, 140, 89, 15);
		add(lblSocketport);

		textField_socketPort = new JTextField();
		textField_socketPort.setColumns(10);
		textField_socketPort.setBounds(165, 137, 155, 21);
		textField_socketPort.setText(ConfUtil.getString("socketPort", ""));
		add(textField_socketPort);

		JButton button = new JButton("保存");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Properties p = ConfUtil.getProperties();
				p.put("ip", textField_ip.getText());
				p.put("httpPort", textField_httpPort.getText());
				p.put("socketPort", textField_socketPort.getText());
				p.put("serverName", textField_appName.getText());
				p.put("groupName", textField_groupName.getText());
				p.put("userName", textField_userName.getText());
				p.put("passWord", textField_password.getText());
				p.put("connectionType", comboBox_connectionType.getSelectedItem().toString());
				p.put("socketSSL", comboBox.getSelectedItem().toString());
				p.put("socketSSLPort", textField_1.getText());
				PropertiesUtil.writeToFile(new File(ConfUtil.confFile), p);
				ConfUtil.reload();
				JOptionPane.showMessageDialog(null, "保存成功", "info", JOptionPane.INFORMATION_MESSAGE);
			}
		});
		button.setBounds(322, 318, 93, 23);
		add(button);

		JLabel label = new JLabel("服务名:");
		label.setHorizontalAlignment(SwingConstants.RIGHT);
		label.setBounds(367, 93, 89, 15);
		add(label);

		textField_appName = new JTextField();
		textField_appName.setColumns(10);
		textField_appName.setBounds(492, 90, 155, 21);
		textField_appName.setText(ConfUtil.getString("serverName", ""));
		add(textField_appName);

		JLabel label_1 = new JLabel("组名:");
		label_1.setHorizontalAlignment(SwingConstants.RIGHT);
		label_1.setBounds(367, 55, 89, 15);
		add(label_1);

		textField_groupName = new JTextField();
		textField_groupName.setColumns(10);
		textField_groupName.setBounds(492, 52, 155, 21);
		textField_groupName.setText(ConfUtil.getString("groupName", ""));
		add(textField_groupName);

		JLabel label_2 = new JLabel("用户名:");
		label_2.setHorizontalAlignment(SwingConstants.RIGHT);
		label_2.setBounds(40, 233, 89, 15);
		add(label_2);

		textField_userName = new JTextField();
		textField_userName.setColumns(10);
		textField_userName.setBounds(165, 230, 155, 21);
		textField_userName.setText(ConfUtil.getString("userName", ""));
		add(textField_userName);

		JLabel label_3 = new JLabel("密码:");
		label_3.setHorizontalAlignment(SwingConstants.RIGHT);
		label_3.setBounds(367, 233, 89, 15);
		add(label_3);

		textField_password = new JTextField();
		textField_password.setColumns(10);
		textField_password.setBounds(492, 230, 155, 21);
		textField_password.setText(ConfUtil.getString("passWord", ""));
		add(textField_password);

		JLabel label_4 = new JLabel("连接方式:");
		label_4.setHorizontalAlignment(SwingConstants.RIGHT);
		label_4.setBounds(367, 137, 89, 15);
		add(label_4);

		String[] data = new String[] { "Socket", "Http", "WebService" };
		comboBox_connectionType = new JComboBox(data);
		comboBox_connectionType.setBounds(490, 137, 155, 21);
		comboBox_connectionType.setSelectedItem(ConfUtil.getString("connectionType", ""));
		add(comboBox_connectionType);

		JLabel lblSocketSsl = new JLabel("Socket SSL:");
		lblSocketSsl.setHorizontalAlignment(SwingConstants.RIGHT);
		lblSocketSsl.setBounds(40, 183, 89, 15);
		add(lblSocketSsl);

		JLabel lblSsl = new JLabel("SSL 端口:");
		lblSsl.setHorizontalAlignment(SwingConstants.RIGHT);
		lblSsl.setBounds(367, 183, 89, 15);
		add(lblSsl);

		textField_1 = new JTextField();
		textField_1.setText(ConfUtil.getString("socketSSLPort", ""));
		textField_1.setColumns(10);
		textField_1.setBounds(492, 180, 155, 21);
		add(textField_1);

		comboBox = new JComboBox(new Object[] { true, false });
		comboBox.setBounds(165, 180, 155, 21);
		add(comboBox);
		comboBox.setSelectedItem(ConfUtil.getBoolean("socketSSL", false));
		this.setVisible(true);
		init();
	}

	private void init() {

	}
}
